#include "eGUI.hpp"
eGUI egui;

eGUI::eGUI(void)
{
    this->cleanFlag = 1;
    listStaticCreate(&this->pageStack);
}

void eGUI::clean(void)
{
    BSP_LCD_Clear(LCD_COLOR_WHITE);
}

void eGUI::run(void)
{
    
    if (this->cleanFlag)
    {
        this->clean();
        this->cleanFlag = 0;
    }
    if (this->page == NULL || this->page->autoFlash == false) return;
    if (this->page->viewcb)
        this->page->viewcb(this->page);
    switch (this->page->type)
    {
    case eGUI_PAGE_TYPE_TEXTLIST:
        this->page->u.textList->show();
        break;
    case eGUI_PAGE_TYPE_GRAPH:
        //TODO: drawCallback and Template Draw
        break;
    case eGUI_PAGE_TYPE_INPUTBOX:
        this->page->u.inputBox->draw();
        break;
    default:
        break;
    }

}

void eGUI::input(uint32_t args)
{
    if (this->page == NULL) return;
    switch (this->page->type)
    {
    case eGUI_PAGE_TYPE_INPUTBOX:
        this->page->u.inputBox->ctrlcb(args);
        break;
    
    default:
        if (this->ctrlcb)
        {
            this->ctrlcb(args);
        }
        break;
    }
}

eGUI_State eGUI::jumpPush(eGUI_Page *Page, bool clear)
{
    if (listPushh(&this->pageStack, this->page))
    {
        this->page = Page;
        if (clear) this->cleanFlag = 1;
        this->ctrlcb = this->page->ctrlcb;
        eGUI_LOG("eGUI jumpPush %p\n", Page);
    }
    else
    {
        eGUI_LOG("eGUI jumpPush error\n");
        return eGUI_ERROR;
    }
    return eGUI_OK;
}
eGUI_State eGUI::jumpPush(eGUI_Page *Page, void(*Ctrlcb)(uint32_t), bool clear)
{
    this->ctrlcb = Ctrlcb;
    return this->jumpPush(Page, clear);
}

eGUI_State eGUI::jumpPop(bool clear)
{
    if (this->pageStack.len == 0)
    {
        return eGUI_ERROR;
    }
    this->page = (eGUI_Page*)listPoph(&this->pageStack);
    this->ctrlcb = this->page->ctrlcb;
    if (clear) this->cleanFlag = 1;
    eGUI_LOG("eGUI jumpPop %p\n", this->page);
    return eGUI_OK;
}
